﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Growatt.OutPutDataProcess;
using Growatt.WaveData.DataSetResolve;
using LEDVANCE.Oscilloscope.VirtualOscilloscope.DataGridViewHelper;
using Newtonsoft.Json;
using Sunny.UI;
using Vosc;
using Vosc.VirtualOscilloscope;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Growatt.WaveData.VirtualOscilloscope
{
    public partial class FrmFindAddress : Form
    {
        private UIComboDataGridView comboBox_Selecet = new UIComboDataGridView()
        {
            DropDownWidth = 500,
            ForeColor = Color.Blue,
            DropDownStyle = UIDropDownStyle.DropDown,
            Font = new System.Drawing.Font(
                "宋体",
                8F,
                System.Drawing.FontStyle.Bold,
                System.Drawing.GraphicsUnit.Point,
                ((byte)(134))
            ),
        };
        //public DataGridView dataGridView1 = null;

        private string _dataFilePath = "configData.json";
        private readonly ModelSaveString cuurentSelectItem;
        private readonly Action<ModelSaveString> callBack;
        DatagirdViewHelper datagirdViewHelper = null;

        public FrmFindAddress(ModelSaveString cuurentSelectItem, Action<ModelSaveString> CallBack)
        {
            InitializeComponent();
            panel1.Controls.Add(comboBox_Selecet);
            comboBox2.Items.AddRange(Enum.GetNames(typeof(ENUM_001)).ToArray());
            comboBox3.Items.AddRange(Enum.GetNames(typeof(ENUM_Ratio)).ToArray());
            this.cuurentSelectItem = cuurentSelectItem;
            callBack = CallBack;
            if (cuurentSelectItem != null)
            {
                textBox_Offset.Text = cuurentSelectItem.SignalOffset;
                textBox1.Text = cuurentSelectItem.Address;
                comboBox2.Text = cuurentSelectItem.Type;
                comboBox3.Text = cuurentSelectItem.Scale;
                comboBox1.Text = cuurentSelectItem.Name;
                comboBox_Selecet.Text = cuurentSelectItem.Name;
            }
            this.FormClosed += Form2_FormClosed;
            comboBox_Selecet.DataGridView.Init();
            comboBox_Selecet.DataGridView.MultiSelect = true; //设置可多选
            comboBox_Selecet.ItemSize = new System.Drawing.Size(500, 200);

            comboBox_Selecet.DataGridView.AddColumn("参数名称", "Name");
            comboBox_Selecet.DataGridView.AddColumn("类型", "Type", fillWeight: 20);
            comboBox_Selecet.DataGridView.AddColumn("倍率", "Scale", fillWeight: 20);
            comboBox_Selecet.DataGridView.AddColumn("地址", "Address", fillWeight: 30);
            //comboBox_Selecet.DataGridView.AddColumn("偏移", "SignalOffset", fillWeight: 20);
            //comboBox_Selecet.DataGridView.AddCheckBoxColumn(
            //    "删除",
            //    "IsSelected",
            //    fillWeight: 30,
            //    readOnly: false
            //);
            comboBox_Selecet.DataGridView.ReadOnly = false;
            comboBox_Selecet.ShowFilter = false;

            comboBox_Selecet.Filter1by1 = false;
            datagirdViewHelper = new DatagirdViewHelper(comboBox_Selecet.DataGridView);
            HideControlsWithTextHelper.HideControlsWithText(comboBox_Selecet.item, "删除");
            HideControlsWithTextHelper.HideControlsWithText(comboBox_Selecet.item, "搜索");
            //点击删除按钮

            comboBox_Selecet.item.DelItems += () =>
            {
                //var data = datagirdViewHelper.GetModels();
                //for (global::System.Int32 j = 0; j < data.Count; j++)
                //{
                //    var delModel = data[j];
                //    datagirdViewHelper.DeleteModel(delModel);
                //}
                label_status.Text = ("删除成功!");
            };
            comboBox_Selecet.ShowText = true;
            comboBox_Selecet.BackColor = Color.Red;
            comboBox_Selecet.ForeColor = Color.Gray;
            var font = new System.Drawing.Font(
                "微软雅黑",
                8F,
                System.Drawing.FontStyle.Regular,
                System.Drawing.GraphicsUnit.Point,
                ((byte)(134))
            );
            comboBox_Selecet.Font = font;


            // 绑定事件
            comboBox_Selecet.ValueChanged += new Sunny.UI.UIComboDataGridView.OnValueChanged(
                this.uiComboDataGridView2_ValueChanged
            );
            comboBox1.SelectedIndexChanged += (s, e) =>
            {
                try
                {
                    int index = comboBox1.SelectedIndex;
                    if (index != -1)
                    {
                        textBox1.Text = DatagirdViewHelper.modelList[index].Address;
                        comboBox2.Text = DatagirdViewHelper.modelList[index].Type;
                        comboBox3.Text = DatagirdViewHelper.modelList[index].Scale;
                    }
                }
                catch (Exception ex) { }
            };
            panel1.Width = 326;
            comboBox_Selecet.Dock = DockStyle.Top;
            this.StartPosition = FormStartPosition.CenterScreen;
            comboBox_Selecet.ForeColor = Color.Black;
            comboBox_Selecet.Font = new System.Drawing.Font(
                "微软雅黑",
                10F,
                System.Drawing.FontStyle.Regular,
                System.Drawing.GraphicsUnit.Point,
                ((byte)(134))
            );
            comboBox_Selecet.Filter1by1 = false;
            comboBox_Selecet.FilterChanged+= UiComboDataGridView2_FilterChanged;

            //this.FormClosed += (_, __) =>
            //{
            //    datagirdViewHelper.SaveDataToJSON(); //关闭前在保存
            //};
        }

        private void UiComboDataGridView2_FilterChanged(object sender, UIComboDataGridViewArgs e)
        {
            datagirdViewHelper.SearchModels(e.FilterText);
        }

        private void uiComboDataGridView2_ValueChanged(object sender, object value)
        {
            if (value != null && value is DataGridViewSelectedRowCollection)
            {
                DataGridViewSelectedRowCollection collection =
                    (DataGridViewSelectedRowCollection)value;
                foreach (var item in collection)
                {
                    DataGridViewRow row = (DataGridViewRow)item;
                    ModelSaveString selectParaInfo = row.DataBoundItem as ModelSaveString;
                    textBox1.Text = selectParaInfo.Address;
                    textBox_Offset.Text = selectParaInfo.SignalOffset;
                    comboBox2.Text = selectParaInfo.Type;
                    comboBox3.Text = selectParaInfo.Scale;
                    itemConfigCurrent = selectParaInfo;
                    comboBox_Selecet.Text = selectParaInfo.Name;
                }
            }
        }

        ModelSaveString itemConfigCurrent = null;

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {

            itemConfigCurrent = DatagirdViewHelper.modelList.FirstOrDefault(o => o.Name.Equals(comboBox_Selecet.Text));
            if (itemConfigCurrent != null)
            {
                callBack.Invoke(itemConfigCurrent);
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            label_status.Text = string.Empty;
            textBox1.Text = string.Empty;
            VariableInfo variableInfo = new VariableInfo(comboBox_Selecet.Text, string.Empty);
            DwarfHelper.DwarfHelperInstance.GetAddress(new List<VariableInfo>() { variableInfo });
            if (!string.IsNullOrEmpty(variableInfo.Address))
            {
                textBox1.Text = variableInfo.Address;
            }
            else
            {
                label_status.Text = ("查询失败");
            }

            var typeString = string.Empty;

            if (!string.IsNullOrEmpty(variableInfo.SzieVar))
            {
                typeString = variableInfo.SzieVar;
                ENUM_001 ty_ENUM_001 = ENUM_001.I16;
                if (typeString.Contains("unsigned"))
                {
                    ty_ENUM_001 = ENUM_001.U16;
                    comboBox3.SelectedIndex = 0;
                }
                else if (typeString.Contains("float"))
                {
                    ty_ENUM_001 = ENUM_001.F32;
                }
                else
                {
                    comboBox3.SelectedIndex = 0;
                    ty_ENUM_001 = ENUM_001.I16;
                }
                comboBox2.Text = Enum.GetName(typeof(ENUM_001), ty_ENUM_001);
            }

            if (!string.IsNullOrEmpty(comboBox_Selecet.Text))
            {
                var datalist = DatagirdViewHelper.modelList.FirstOrDefault(o =>
                    o.Name.Equals(comboBox_Selecet.Text)
                );
                if (datalist == null)
                {
                    var data = new ModelSaveString
                    {
                        Name = comboBox_Selecet.Text,
                        Address = textBox1.Text,
                        Type = comboBox2.Text,
                        Scale = comboBox3.Text,
                        //SignalOffset = textBox_Offset.Text,
                    };
                    datagirdViewHelper.AddModel(data);
                    label_status.Text = ("添加成功!");
                    label_status.ForeColor = Color.Blue;
                }
                else
                {
                    datalist.Address = textBox1.Text;
                    datalist.Type = comboBox2.Text;
                    datalist.Scale = comboBox3.Text;
                    //datalist.SignalOffset = textBox_Offset.Text;
                    datagirdViewHelper.AddModel(datalist);
                    label_status.Text = ("参数已存在,修改成功!");
                    label_status.ForeColor = Color.Green;
                }
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            var datalist = DatagirdViewHelper.modelList.FirstOrDefault(o =>
                o.Name.Equals(comboBox_Selecet.Text)
            );
            if (datalist != null)
            {
                datagirdViewHelper.DeleteModel(datalist);
                label_status.Text = ("删除成功!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            datagirdViewHelper.DeleteAllModels();
            label_status.Text = ("删除成功!");
        }

    }
}
